﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ecopsy.Tools;
using AgencyEntityLib.BaseEntity;
using FormControllers;
using Ecopsy.DataTier;
using SqlLibrary;
using Ecopsy.DataTier.SqlLibrary;

public partial class iCRM_Clients : iCRM_Page
{    
    wsFace _ws_Face = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Where.ListLoadStatment.ParamValueList.Clear();
            if (CategoriaID.Length > 0)
            {                 
                Where.ListLoadStatment.ParamValueList_SearchOption = vPropertyOfList.Options.Value; 
                Where.ListLoadStatment.ParamValueList.Add(new vPropertyOfList.ParamListItem() { Param = string.Empty, Value = CategoriaID });
            }

            MyBind();
        }

        OnInit();
    }

    protected void OnInit()
    {
        ctlFilterSave1.OkOnClick = ctlFilterSave1_OkOnClick;
        ctlFilterSave1.FilterType = Filter.FilterType.ClientList;
        ctlFilterSave1.SaveOnClick = ctlFilterSave1_SaveOnClick;
        ctlFilterSave1.AddKey = CategoriaID;

        ctlFilterBar1.OnFindClik = ctlFilterBar1_OnFindClik;
        ctlFilterBar1.OnFilterListClik = ctlFilterBar1_OnFilterListClik;
        ctlFilterBar1.HintServiceMethod = "GetClientListParams";

        ctlFilterList1.OnApp_Click = ctlFilterList1_OnApp_Click;
        ctlFilterList1.FilterType = Filter.FilterType.ClientList;
        ctlFilterList1.AddKey = CategoriaID;

        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterList.DataBind();
    }

    private bool ctlFilterList1_OnApp_Click(ListWhere _Where)
    {
        Where = _Where;

        //ctlFilterSave1.DataBind();
        //ctlFilterBar1.DataBind();
        //lvClients.DataBind();
        MyBind();
        return true;
    }

    private bool ctlFilterBar1_OnFilterListClik()
    {
        ctlFilterList1.MyDataBind();
        ModalPopupext_FilterList.Show();
        return true;
    }

    private bool ctlFilterSave1_OkOnClick()
    {
        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterSave.Hide();
        return true;
    }

    private ListWhere ctlFilterSave1_SaveOnClick()
    {
        return (ListWhere)Where.Clone();
    }


    private bool ctlFilterBar1_OnFindClik(string sFindStr)
    {
        Where.ListLoadStatment .Lexeme = ctlFilterBar1.Lexeme;
        MyBind();
        return true;
    }


    #region SavePanel

    protected string GetTargetControlID_FilterSave()
    {
        return ctlFilterBar1.BtnAddFilter_ClientID;
    }

    public string GetOkControlID_FilterSave()
    {

        return ctlFilterSave1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterSave()
    {

        return ctlFilterSave1.BtnCancel_ClientID;
    }

    #endregion

    #region ListPanel

    protected string GetTargetControlID_FilterList()
    {
        return ctlFilterBar1.BtnFilterList_ClientID;
    }

    public string GetOkControlID_FilterList()
    {

        return ctlFilterList1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterList()
    {

        return ctlFilterList1.BtnCancel_ClientID;
    }

    #endregion


    private string CategoriaID 
    {
        get 
        {
            if (null == Request.QueryString[qCategoriaID]) return string.Empty;
            return Request.QueryString[qCategoriaID]; 
        }
    }
 
    private void MyBind()
    {
        ObjectDS_PF ODS = new ObjectDS_PF(GetLvList);
        lvClients.DataSource = ODS.ImAsObjectDataSource;
        DataBind();
    }
        
    protected grdClient[] GetLvList(int startRowIndex, int maximumRows, out int RowCount)
    {
        Client[] l  =GetList(Where, out RowCount, startRowIndex, maximumRows);   
        grdClient[] r = new grdClient[l.Length];
        for (int i = 0; i < r.Length; i++)
        {
            r[i] = new grdClient (l[i]);
        }


        //заряжаем comparer функцией сортировки 
        Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        // Сортируем
        Array.Sort(r, Where.SortCondition);
        return r;
         
            
     
    }
    

    public Client [] GetList(
                                           object _Where,
                                           out int RowCount,
                                           int startRowIndex,
                                           int endRowIndex
                                       )
    {

        ListWhere w = (ListWhere)_Where;
        w.ListLoadStatment.StartRowIndex = startRowIndex;
        w.ListLoadStatment.EndRowIndex = endRowIndex;        
        ClientList l = _ws_Face.GetClientList((ClientList.sqlLoadStatment )w.ListLoadStatment);
        RowCount = l.OutputRwCnt;
        return l.ToList<Client>().ToArray<Client>();

    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        string s = "PageProcessor.aspx?Page=iCRM_ClientEditor.aspx?ClientID=" + GlobalConstants.qPrefNEW_RECORD + Guid.NewGuid().ToString() + GlobalConstants.qParamSplitter;
        Response.Redirect(s);
        
    }
 
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        
    }

    protected string GetEditCommand(string ID)
    {
        string s = "PageProcessor.aspx?Page=iCRM_ClientEditor.aspx?ClientID=" + ID;
        return "document.location = '" + s + "'";
        

    }
    
    protected void lvClients_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        ListView lv = (ListView)sender;
        switch (e.CommandName)
        {
            case "Edit":
                {

                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lv.EditIndex = this.DataPager.CalculateRealEditIndex(d.DataItemIndex);

                    break;
                }
            case "Insert":
                {

                    break;
                }
            case "Update":
                {
                    lv.EditIndex = -1;
                    break;
                }
            case "Delete":
                {
                    //DeleteCustomer(e.CommandArgument as string);
                    break;
                }
            case "Cancel":
                {
                    lv.EditIndex = -1;
                    break;
                }
        }

        ObjectDS_PF ODS = new ObjectDS_PF(GetLvList);
        lvClients.DataSource = ODS.ImAsObjectDataSource;
        lv.DataBind();
        



    }

    protected grdClient Get_grdClient(String ID)    
    {
        return new grdClient (_ws_Face.GetClient(ID)); 
    }

    protected void lvClients_ItemEditing(object sender, ListViewEditEventArgs e)
    {

    }
   
    protected void lvClients_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {

    }
   
    protected void lvClients_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
         
        if (null == e.Item.FindControl("cmdCloseClientShow")) return;
        GridView grd = (GridView)e.Item.FindControl("grdContactFaces");
        ListViewDataItem d = (ListViewDataItem)e.Item;
        Client c = ((grdClient)d.DataItem).Client ;
        string id = c.ID;
        c = new Client() { ID = id };
        c.Load();
    }
    
    protected void lvClients_SelectedIndexChanged(object sender, EventArgs e)
    {
      
    }
   
    protected void lvClients_PagePropertiesChanged(object sender, EventArgs e)
    {
        MyBind();
    }
    
    

    protected void lvSort(object sender, EventArgs e)
    {
        f_lvSort(sender, lvClients, MyBind); 
      
    }

    protected override ListWhere Where
    {
        get
        {
            ClientList.sqlLoadStatment st=new ClientList.sqlLoadStatment();            
            return f_Where(st, "Name");
        }

        set
        {
            f_SetWhere(value);
        }
    }

    protected void btnAddClient_Click(object sender, EventArgs e)
    {
        string s = "PageProcessor.aspx?Page=iCRM_ClientEditor.aspx?ClientID=" + GlobalConstants.qPrefNEW_RECORD + Guid.NewGuid().ToString() + GlobalConstants.qParamSplitter;
        Response.Redirect(s);

    }

    protected void EditClient(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        bool isEnabled = true;
        string sUrlKey = "iCRM_ClientEditor";
        iPageController.SetPageUrl(ref sUrlKey, ref isEnabled);
        string s = "PageProcessor.aspx?Page=" + sUrlKey + ".aspx?" + "ClientID=" + btn.CommandArgument;
        Response.Redirect(s);
    }

    
}
